﻿using System.IO;
using NHibernate;
using NHibernate.Cfg;
using NUnit.Framework;
using NHConfiguration;
namespace NHTest
{
    public abstract class NhibernateTest
    {
        private static ISessionFactory _sessionFactory;
        private  ISession _session;
        private  ITransaction _transaction;

        protected ISession Session
        {
            get
            {
                return _session;
            }
        }
        [SetUp]
        public void SetUp()
        {
            if(_sessionFactory == null)
            {
                var cfg = NhibernateConfigurationSection.CurrentConfiguration;
                var configuration = new Configuration();
                configuration.Configure(cfg.HibernateFile);
                configuration.AddDirectory(new DirectoryInfo(cfg.MappingPath));
                _sessionFactory = configuration.BuildSessionFactory();
            }
            _session = _sessionFactory.OpenSession();
            _transaction = _session.BeginTransaction();
        }

        [TearDown]
        public void TearDown()
        {
            _transaction.Rollback();
            _transaction.Dispose();
            _transaction = null;
            _session.Close();
            _session.Dispose();
            _session = null;
        }
    }
}
